#include "rrdiffuse.h"
#include <cmath>

RRDiffuse::RRDiffuse()
{
    lightX = -1.0f;
    lightY = -4.0f;
    lightZ = 0.0f;

    for(int i=1; i<4; i++) {
        Kdiff[i] = 0.8f;
        Ldiff[i] = 1.0f;
    }
    Kdiff[0] = 1.0f;
    Ldiff[0] = 1.0f;
}

void RRDiffuse::rrDiffue(RRPrim *arg)
{
    RRVertex *vp;
    float ToLight[3];
    float len;
    float ip;
    int vnum;
    vnum = arg->rrGetVertsNum();
    for(int i=0; i<vnum; i++) {
        vp = arg->rrGetVertAdrs(i);
        ToLight[0] = lightX - vp->x;
        ToLight[1] = lightY - vp->y;
        ToLight[2] = lightZ - vp->z;
        len = sqrtf(ToLight[0] * ToLight[0] + ToLight[1] * ToLight[1] + ToLight[2] * ToLight[2]);
        ip = (ToLight[0] * vp->nx + ToLight[1] * vp->ny + ToLight[2] * vp->nz) / len;
        if(ip < 0) ip = 0;
        for(int i=1; i<4; i++)
            vp->c[i] = vp->c[i] * ip * Kdiff[i] * Ldiff[i];
    }
}
